package com.question.mapper;

import com.github.pagehelper.Page;
import com.question.annotation.AutoFill;
import com.question.enumeration.OperationType;
import com.question.pojo.dto.UserStats;
import com.question.pojo.entity.User;
import com.question.pojo.vo.UserVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    @Select("select user_id, username, password_hash, is_admin from user where username = #{param1} and user_id != #{param2}")
    User getByUsername(String username, Long userId);

    /**
     * 创建用户
     * @param user
     */
    @AutoFill(OperationType.CREATED_AT)
    @Insert("insert into user(username, password_hash, email, created_at, is_admin, professional) values(#{username}, #{passwordHash}, #{email}, #{createdAt}, #{isAdmin}, #{professional})")
    void insert(User user);

    /**
     * 查询邮箱是否存在
     * @param email
     * @return
     */
    @Select("select * from user where email = #{param1} and user_id != #{param2}")
    Boolean findByEmail(String email, Long userId);

    /**
     * 修改用户信息
     * @param user
     */
    void modify(User user);

    /**
     * 根据id查询用户
     * @param userId
     * @return
     */
    @Select("select * from user where user_id = #{userId}")
    User getById(Long userId);













    @Select({
            "<script>",
            "SELECT user_id, username, email, created_at, is_admin, professional",
            "FROM user",
            "<where>",
            "   <if test='professional != null and professional != \"\"'>",
            "       AND professional = #{professional}",
            "   </if>",
            "   <if test='isAdmin != null'>",
            "       AND is_admin = #{isAdmin}",
            "   </if>",
            "   <if test='keyword != null and keyword != \"\"'>",
            "       AND (username LIKE CONCAT('%', #{keyword}, '%')",
            "       OR email LIKE CONCAT('%', #{keyword}, '%')",
            "       OR professional LIKE CONCAT('%', #{keyword}, '%'))",
            "   </if>",
            "</where>",
            "ORDER BY created_at DESC",
            "LIMIT #{offset}, #{pageSize}", // 确保添加分页参数
            "</script>"
    })
    List<UserVO> selectUserPage(@Param("keyword") String keyword,
                                @Param("isAdmin") Boolean isAdmin,
                                @Param("professional") String professional,
                                @Param("offset") Integer offset,
                                @Param("pageSize") Integer pageSize);

    // 修改统计方法，保持参数一致
    @Select({
            "<script>",
            "SELECT COUNT(*) FROM user",
            "<where>",
            "   <if test='keyword != null and keyword != \"\"'>",
            "       AND (username LIKE CONCAT('%', #{keyword}, '%')",
            "       OR email LIKE CONCAT('%', #{keyword}, '%')",
            "       OR professional LIKE CONCAT('%', #{keyword}, '%'))",
            "   </if>",
            "   <if test='isAdmin != null'>",
            "       AND is_admin = #{isAdmin}",
            "   </if>",
            "   <if test='professional != null and professional != \"\"'>",
            "       AND professional = #{professional}",
            "   </if>",
            "</where>",
            "</script>"
    })
    int countUserPage(@Param("keyword") String keyword,
                      @Param("isAdmin") Boolean isAdmin,
                      @Param("professional") String professional);

    /**
     * 查询用户数量
     * @param isAdmin
     * @return
     */
    @Select("SELECT COUNT(*) FROM user WHERE is_admin = #{isAdmin}")
    Long countByAdminStatus(@Param("isAdmin") Boolean isAdmin);

    /**
     * 获取用户统计信息
     * @return
     */
    @Select("SELECT " +
            "(SELECT AVG(cnt) FROM (SELECT COUNT(*) AS cnt FROM user_answers GROUP BY user_id) t) AS avgAnswers, " +
            "(SELECT AVG(cnt) FROM (SELECT COUNT(*) AS cnt FROM user_favorites GROUP BY user_id) t) AS avgFavorites")
    UserStats getUserStats();

//    /**
//     * 分页查询用户
//     * @param params
//     * @return
//     */
//    @Select({
//            "<script>",
//            "SELECT user_id, username, email, created_at, is_admin, professional",
//            "FROM user",
//            "<where>",
//            "   <if test='keyword != null and keyword != \"\"'>",
//            "       AND (username LIKE CONCAT('%', #{keyword}, '%')",
//            "       OR email LIKE CONCAT('%', #{keyword}, '%')",
//            "       OR professional LIKE CONCAT('%', #{keyword}, '%'))",
//            "   </if>",
//            "   <if test='isAdmin != null'>",
//            "       AND is_admin = #{isAdmin}",
//            "   </if>",
//            "   <if test='professional != null and professional != \"\"'>",
//            "       AND professional = #{professional}",
//            "   </if>",
//            "</where>",
//            "ORDER BY created_at DESC",
//            "</script>"
//    })
//    Page<UserVO> selectUserPage(Map<String, Object> params);

    @Select("select user_id from user where user_id > #{lastUserId} order by user_id ASC limit #{batchSize}")
    List<Long> selectUsersForWarmUp(int lastUserId, int batchSize);

}
